package com.irdstudio.tdp.console.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.irdstudio.sdk.beans.core.vo.EasyUITreeData;
import com.irdstudio.tdp.console.service.vo.SSubsInfoVO;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.tdp.console.service.facade.PaasSysTreeService;
import com.irdstudio.tdp.console.dao.PaasSysTreeDao;
import com.irdstudio.tdp.console.dao.domain.PaasSysTree;
import com.irdstudio.tdp.console.service.vo.PaasSysTreeVO;
import com.irdstudio.sdk.beans.core.base.FrameworkService;
/**
 * <p>ServiceImpl: 平台系统目录树				<p>
 * @author zjj
 * @date 2021-03-24
 */
@Service("paasSysTreeServiceImpl")
public class PaasSysTreeServiceImpl implements PaasSysTreeService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(PaasSysTreeServiceImpl.class);

	@Autowired
	private PaasSysTreeDao paasSysTreeDao;
	
	/**
	 * 新增操作
	 */
	public int insertPaasSysTree(PaasSysTreeVO inPaasSysTreeVo) {
		logger.debug("当前新增数据为:"+ inPaasSysTreeVo.toString());
		int num = 0;
		try {
			PaasSysTree paasSysTree = new PaasSysTree();
			beanCopy(inPaasSysTreeVo, paasSysTree);
			num = paasSysTreeDao.insertPaasSysTree(paasSysTree);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(PaasSysTreeVO inPaasSysTreeVo) {
		logger.debug("当前删除数据条件为:"+ inPaasSysTreeVo);
		int num = 0;
		try {
			PaasSysTree paasSysTree = new PaasSysTree();
			beanCopy(inPaasSysTreeVo, paasSysTree);
			num = paasSysTreeDao.deleteByPk(paasSysTree);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inPaasSysTreeVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(PaasSysTreeVO inPaasSysTreeVo) {
		logger.debug("当前修改数据为:"+ inPaasSysTreeVo.toString());
		int num = 0;
		try {
			PaasSysTree paasSysTree = new PaasSysTree();
			beanCopy(inPaasSysTreeVo, paasSysTree);
			num = paasSysTreeDao.updateByPk(paasSysTree);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inPaasSysTreeVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public PaasSysTreeVO queryByPk(PaasSysTreeVO inPaasSysTreeVo) {
		
		logger.debug("当前查询参数信息为:"+ inPaasSysTreeVo);
		try {
			PaasSysTree queryPaasSysTree = new PaasSysTree();
			beanCopy(inPaasSysTreeVo, queryPaasSysTree);
			PaasSysTree queryRslPaasSysTree = paasSysTreeDao.queryByPk(queryPaasSysTree);
			if (Objects.nonNull(queryRslPaasSysTree)) {
				PaasSysTreeVO outPaasSysTreeVo = beanCopy(queryRslPaasSysTree, new PaasSysTreeVO());
				logger.debug("当前查询结果为:"+ outPaasSysTreeVo.toString());
				return outPaasSysTreeVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<PaasSysTreeVO> queryAllOwner(PaasSysTreeVO paasSysTreeVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<PaasSysTreeVO> list = null;
		try {
			List<PaasSysTree> paasSysTrees = paasSysTreeDao.queryAllOwnerByPage(paasSysTreeVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ paasSysTrees.size());
			pageSet(paasSysTrees, paasSysTreeVo);
			list = (List<PaasSysTreeVO>) beansCopy(paasSysTrees, PaasSysTreeVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<PaasSysTreeVO> queryAllCurrOrg(PaasSysTreeVO paasSysTreeVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<PaasSysTree> paasSysTrees = paasSysTreeDao.queryAllCurrOrgByPage(paasSysTreeVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+paasSysTrees.size());
		List<PaasSysTreeVO> list = null;
		try {
			pageSet(paasSysTrees, paasSysTreeVo);
			list = (List<PaasSysTreeVO>) beansCopy(paasSysTrees, PaasSysTreeVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<PaasSysTreeVO> queryAllCurrDownOrg(PaasSysTreeVO paasSysTreeVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<PaasSysTree> paasSysTrees = paasSysTreeDao.queryAllCurrDownOrgByPage(paasSysTreeVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ paasSysTrees.size());
		List<PaasSysTreeVO> list = null;
		try {
			pageSet(paasSysTrees, paasSysTreeVo);
			list = (List<PaasSysTreeVO>) beansCopy(paasSysTrees, PaasSysTreeVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 *  根据 业务系统代码检查 目录结构
	 */
	@SuppressWarnings("unchecked")
	public List<EasyUITreeData> querySSubsInfoTree(SSubsInfoVO sSubsInfoVO) {
		List<EasyUITreeData> tree = new ArrayList<>();
		try {
			// 根节点
			PaasSysTreeVO param = new PaasSysTreeVO();
			param.setPackageId(sSubsInfoVO.getSubsCode());
			List<PaasSysTree> paasSysTrees = paasSysTreeDao.queryAllOwner(param);
			for (PaasSysTree el : paasSysTrees) {
				EasyUITreeData treeData = this.fetchTreeChildren(el);
				tree.add(treeData);
			}
			if (!tree.isEmpty()) {
				tree.get(0).setState("open");
			}
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return tree;

	}

	/**
	 * 递归获取树结构
	 * @param paasSysTree
	 * @return
	 */
	private EasyUITreeData fetchTreeChildren(PaasSysTree paasSysTree) {
		EasyUITreeData treeData = new EasyUITreeData();
		treeData.setId(paasSysTree.getPackageId());
		treeData.setText(paasSysTree.getPackageName());
		treeData.setpId(paasSysTree.getPackageAbvId());

		Map<String, Object> attributes = new HashMap<String, Object>();
		attributes.put("package_code", paasSysTree.getPackageCode());
		attributes.put("package_type", paasSysTree.getPackageType());
		attributes.put("package_url", paasSysTree.getPackageUrl());
		treeData.setAttributes(attributes);
		// 检查是否有子节点
		PaasSysTreeVO param = new PaasSysTreeVO();
		param.setPackageAbvId(paasSysTree.getPackageId());
		List<PaasSysTree> paasSysTreeList = paasSysTreeDao.queryAllOwner(param);
		if (CollectionUtils.isNotEmpty(paasSysTreeList)) {
			treeData.setChildren(new ArrayList<>(paasSysTreeList.size()));
			for (PaasSysTree el : paasSysTreeList) {
				treeData.getChildren().add(this.fetchTreeChildren(el));
			}
		}
		return treeData;
	}

}
